<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AlertCreateColumnOrder extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $cFunc = function (Blueprint $table) {
            $table->unsignedInteger('orderShouldPrice')->default(0)->after('originalPrice')
                  ->comment('订单应付金额');
        };

        // 分表库
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlOrder")->table('order' . $this->name, $cFunc);

            \App\Kuafu\Order\Model\OrderSplitModel::suffix($i)
                                                  ->query()
                                                  ->update(['orderShouldPrice' => \Illuminate\Support\Facades\DB::raw('shouldPrice')]);
        }


        $this->name = '';
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        $cFunc = function (Blueprint $table) {
            $table->dropColumn('orderShouldPrice');
        };

        // 分表库
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlOrder")->table('order' . $this->name, $cFunc);
        }
    }
}
